package com.example.admin.controller;


import com.example.admin.common.Result;
import com.example.admin.entity.UserCenter;
import com.example.admin.service.UserCenterService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin")
@CrossOrigin(origins = "http://localhost:8080")
public class UserCenterController {


    @Autowired
    private UserCenterService  userCenterService;


    @GetMapping("/userCenterInfoPage")
    public Result<PageInfo> userCenterInfoPage(@RequestParam(defaultValue = "1") Integer pageNumber,@RequestParam(defaultValue = "10") Integer pageSize) {
        PageHelper.startPage(pageNumber,pageSize);
      List<UserCenter> userCenterList=  userCenterService.userCenterInfoPage();
      PageInfo<UserCenter> pageInfo=new PageInfo<>(userCenterList);
      return Result.success(pageInfo);
    }

    /**
     * 根据id删除
     */


    @DeleteMapping("/deleteUserById/{id}")
    public Result deleteUserById(@PathVariable Integer id){

        Result result=  userCenterService.deleteUserById(id);
        return result;
    }

    /**
     * 新增
     */

    @PostMapping("/addUserInfo")
    public Result addUserInfo(@RequestBody UserCenter userCenter){
       Result result= userCenterService.addUseInfo(userCenter);
       return result;
    }

    /**
     * 按照用户名查询
     */
    @GetMapping("/selectUserByName")
    public Result<PageInfo> selectUserByUserName(@RequestParam String username,@RequestParam Integer pageNumber,@RequestParam Integer pageSize){
        PageHelper.startPage(pageNumber,pageSize);
       List<UserCenter> userCenterList= userCenterService.selectUserByUserName(username);
       return Result.success(new PageInfo(userCenterList));
    }

    /**
     * 编辑用户信息
     */

    @PutMapping("/editeUserInfo")
    public Result editUserInfo(@RequestBody UserCenter userCenter){
       Result result= userCenterService.editUserInfo(userCenter);
       return result;
    }



}
